package com.hmagic.hbase.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;

/**
 *  hbase admin client 客户端测试程序
 *
 *  说明: admin 轻量级、非线程安全的，不建议使用admin线程池，每个线程单独创建客户端即可
 */
public class HbaseAdminUtil {

    private static Connection connection = null;

    private static Configuration conf =null;


    static {
        conf = HBaseConfiguration.create();
        conf.set("zookeeper.znode.parent", "/hbase-unsecure");


        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Admin 类测试
     */
    public  static  void main(String arg0[]){

        //创建命名空间
       /* String namespace = "news";
        try {
            Admin admin = connection.getAdmin();
            admin.createNamespace(NamespaceDescriptor.create(namespace).build());

            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        //创建表
        try {
            Admin admin = connection.getAdmin();
            //添加表名称
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("news:replay_content"));

            //添加family
            hTableDescriptor.addFamily(new HColumnDescriptor("content"));

            //添加列


            admin.createTable(hTableDescriptor);
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }





    }

}
